package com.filter;


import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/*
 *
 *@author XCH
 *@date2025 - 02 - 24
 *
 */
@WebFilter("/api/*")
public class CorsFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig)
            throws ServletException {
        System.out.println("CoresFilter.....");
    }

    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers",
                "Authorization,Origin,X-Requested-With,Content-Type,Accept," +
                        "content-Type,origin,x-requested-with,content-type,accept," +
                        "authorization,token,id,X-Custom-Header,X-Cookie," +
                        "Connection,User-Agent,Cookie,*");
        filterChain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
